package com.shenghui.enterprise.base.controller;
import com.shenghui.enterprise.utils.GeneralUtil;
import com.shenghui.enterprise.vo.Result;
import com.shenghui.enterprise.base.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @Description: 登录控制器
 * @Author houjh
 * @Email: happyxiaohou@gmail.com
 * @Date: 2020-05-29
 * @Copyright (c) 2020-2022 云南昇晖科技有限公司
 */
@Controller
public class AdminLoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 登录页面跳转
     * @return
     */
    @RequestMapping(value = {"/admin/login"},method = RequestMethod.GET)
    public String login(){
        return "/html/admin/base/login/login.html";
    }

    /**
     * 登录功能
     * @param request
     * @param response
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/admin/login",method = RequestMethod.POST)
    @ResponseBody
    public Object login(HttpServletRequest request, HttpServletResponse response, @RequestParam Map map) throws Exception{
        // 根据账号 查询数据
        if (GeneralUtil.isNull(map.get("account"))) {
            return Result.error( "帐号不能为空");
        }
        if (GeneralUtil.isNull(map.get("password"))) {
            return Result.error( "密码不能为空");
        }
        return loginService.login(map,request,response);
    }

    /**
     * 退出登录
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping("/admin/loginOut")
    @ResponseBody
    public Object loginOut(HttpServletRequest request) throws Exception {
        return loginService.loginOut(request);
    }

}
